<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
request.setCharacterEncoding("UTF-8");
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <base href="<%=basePath%>">
<title>合作伙伴配置</title>

<jsp:include page="/WEB-INF/jsp/common/common.jsp"></jsp:include>
<link rel="stylesheet" href="<%=path%>/css/zTreeStyle/zTreeStyle.css">
<script type="text/javascript" src="<%=path%>/js/jquery.ztree.core-3.5.js"></script>
<script type="text/javascript" src="<%=path%>/js/jquery.ztree.excheck-3.5.js"></script>
<script src="<%=path%>/js/jquery.divbox.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
	var Formstatus=true;
	var channelCode = '${channelCode}';
	var deptCode = '${deptCode}';

	history.forward();
	$(document).ready(function() {
		isReadyIframe();
		queryChannelInfo(1);
		loadApp_div();
	});
	function isReadyIframe(){
		var iframe = $(window.parent.document).find("#iframe").length;
		if(iframe==0){
			window.location.href="<%=basePath%>index.jsp";
		}
	}
	
	//加载新增、修改时的应用下拉框
	function loadApp_div(){
		$.ajax({
				url : "<%=path%>/channelManage/queryAllAppList?"+new Date().getTime(),
				type : 'POST',
				async : true,
				dataType:'json',
				success : function(appList) {
					if($.validate(appList))  return;
  					//业务处理
					//应用下拉框对象
					var appSelect_div = document.getElementById("appSelect_div");
					appSelect_div.options.length = 0;
					if (appList != null) {
						for (var i = 0; i < appList.length; i++) {
							var option = new Option();
							option.text = appList[i].appname;
							option.value = appList[i].appid;
							appSelect_div.add(option);
						}
					}
				}
			});
	 }

	//点击“查询”
	function queryChannelInfo(pageIndex){
		$.openLoading();
		//按照条件执行查询操作
		$.ajax({
			url : "<%=path%>/channelManage/queryAppInfoByChannelCode?"+new Date().getTime(),
			type : 'POST',
			async : true,
			data:{
				channelCode:channelCode,
				pageIndex:pageIndex
			},
			dataType:'json',
			success:function(channelInfoPage){
				$.closeLoading();
				
				if($.validate(channelInfoPage))  return;
				
				if(null != channelInfoPage){
					var buttonStr = "<a href='' onClick='viewChannelAppInfo(this);return false'>编辑</a> <a href='' onClick='deleteChannelAppInfo(this);return false'>删除</a>";
					var fieldNames = "CHANNEL_CODE@TDH,APPNAME,APPID@TDH,TRANS_TYPE?[{'1':'界面出单'};{'2':'系统对接'}],TRANS_TYPE@TDH";
					
					var tableContent = creatTable1(fieldNames,channelInfoPage,null,buttonStr);
					var tableObj = $("#channelInfoTable");
					tableObj.find("tbody").remove();
					tableObj.append(tableContent);
					
					var strPage = creatChannelPage(pageIndex,6,channelInfoPage.maxPage,"1");
					var pageDIV = $("#channelInfoPageDIV");
					pageDIV[0].innerHTML="";
					pageDIV.append(strPage);
				}
			}
		});
	}
	
	//点击“删除”（channelInfo）
	function deleteChannelAppInfo(channelInfo){
		//要删除的合作伙伴代码
		var channelCode = $(channelInfo).parent('td').parent('tr')[0].childNodes[0].innerHTML;
		var appId = $(channelInfo).parent('td').parent('tr')[0].childNodes[2].innerHTML;
			$.dialog({
				type: "warn",//success,error
				content: "您确定要删除该条记录吗？",
				onOk: function() {
					$.openLoading();
					 $.ajax({
							url : "<%=path%>/channelManage/deleteChannelAppInfo?"+new Date().getTime(),
							type : 'POST',
							dataType:'json',
							data:{
								channelCode:channelCode,
								appId:appId,
								deptCode:deptCode
							},
							success : function(data) {
								$.closeLoading();
								
								if($.validate(data))  return;
								
								queryChannelInfo(1);
							}
						});	 
				}
			});
		}
	
	//点击"新增"(弹出层)
	function addChannelAppInfo(){
		$("#appSelect_div").bind("change",function (){checkIsExist()});
		displayChannelInfoRelatedDiv();
		$("#addOrUpdateDiv").OpenDiv();
		$("#saveChannelInfoBtn").val("新增");
		disabledBtn("channelTranstypeBtn");
	}
	
	//新增channelAppInfo时，检查系统中是否已经存在该应用
	var channelAppIsExistFlag = true;
	function checkIsExist(){
		var appId = $("#appSelect_div").val();
		 $.ajax({
				url : "<%=path%>/channelManage/queryChannelAppInfoByCodeAndId?"+new Date().getTime(),
				type : 'POST',
				async:false,
				dataType:'json',
				data:{
					channelCode:channelCode,
					appId:appId
				},
				success :  function(data) {
					if(data != null && data.result == "fail"){//错误
						$.message("error", "系统错误");
					}else if(data != null){//业务处理
						channelAppIsExistFlag = false;
						$.message("error", "该应用已经添加，请勿重复添加或更新");
 					}else if(data == null){
						channelAppIsExistFlag = true;
					}
				}
			});	 
	}
	
	//显示 合作伙伴相关配置
	function displayChannelInfoRelatedDiv(){
		$("#channelRelatedTable").css("border-bottom","1px solid #bbcddb");
		$("#addOrUpdateDiv").css("height","280px");
		$("#channelRelatedDiv").css("height","260px");
	}
	
	var appId_present;//记录当前channelInfo所属的app
	//点击“保存/更新(channelInfo)”
	function saveOrUpdateChannelInfo(){
		var appSelect_div = $("#appSelect_div").val();
		var saveChannelInfoBtn = $("#saveChannelInfoBtn").val();
		var saveOrUpdate;
		if(saveChannelInfoBtn == "新增"){
			saveOrUpdate = "save";
		}
		if(saveChannelInfoBtn == "更改"){
			saveOrUpdate = "update";
		}
		//检查是否存在该应用
		checkIsExist();
		if(!channelAppIsExistFlag){
			return;
		}
		$.openLoading();
		$.ajax({
			url : "<%=path%>/channelManage/saveOrUpdateChannelAppInfo?"+new Date().getTime(),
			type : 'POST',
			async : true,
			data:{
				channelCode:channelCode,
				appId:appSelect_div,
				preAppId:appId_present,
				flag:saveOrUpdate,
				deptCode:deptCode
			},
			dataType:'json',
			success : function(data) {
				$.closeLoading();
				if($.validate(data))  return;
				//业务处理
				$("#appSelect_div").val(appSelect_div);
				$("#saveChannelInfoBtn").val("更改");
				appId_present = appSelect_div;
				queryChannelInfo(1);//刷新channelInfo的表格内容
				loadChannelTranstype(channelCode,appId_present);// 刷新交易类型表格
				loadChannelTranstypeSelect();//更改所属应用后重新加载channelTranstypeSelect
			}
		});
	}
	
	//点击“编辑”（channelInfo）
	function viewChannelAppInfo(channelInfo){
			var tempTr = $(channelInfo).parent().parent()[0];
			var channelCode= tempTr.childNodes[0].innerHTML;
 			appId_present = tempTr.childNodes[2].innerHTML;
		    var transType = tempTr.childNodes[4].innerHTML;
			$("#addOrUpdateDiv").OpenDiv();
			$("#appSelect_div").val(appId_present);
			$("#saveChannelInfoBtn").val("更改");
			enabledBtn("channelTranstypeBtn");
			
			loadChannelTranstype(channelCode,appId_present);//加载channelTranstype
			
			displayChannelInfoRelatedDiv();
	}
	
	//关闭弹出层
	function closeAddOrUpdateDiv(){
		$("#addOrUpdateDiv").CloseDiv();
		appId_present = '';
		$("#addOrUpdateDiv input[type!='button']").val("");
		$("select").val("");
		$("#channelTranstypeTable").find("tbody").remove();
	}
	
	//加载channelTranstype信息
	function loadChannelTranstype(channelCode,appid){
		$.ajax({
			url : "<%=path%>/channelManage/queryChannelTranstypeByFkChannelCode?"+new Date().getTime(),
			type : 'POST',
			async : true,
			data:{
				fkChannelCode:channelCode,
				appid:appid
			},
			dataType:'json',
			success : function(data) {
				
				if($.validate(data))  return;
				
				//业务处理
				if(null != data){
					var buttonStr = "<a href='' onClick='delChannelTranstype(this);return false'>删除</a>";
					var fieldNames = "transtypeId@TDH,transName";
					var tableContent = creatTable(fieldNames,data,buttonStr);
					var tableObj = $("#channelTranstypeTable");
					tableObj.find("tbody").remove();
					tableObj.append(tableContent);
				}
			
				loadChannelTranstypeSelect();	//加载channelTranstype下拉框			
			}
		});
	}
	
	//加载channelTranstype下拉框(只加载当前channel中没有，而当前channel所在的app中有的transtype)
	function loadChannelTranstypeSelect(){
		$.ajax({
			url : "<%=path%>/channelManage/queryNotUsedAppTranstype?"+new Date().getTime(),
			type : 'POST',
			async : false,
			data:{
				appId:appId_present,
				channelCode:channelCode
			},
			dataType:'json',
			success : function(data) {
				if($.validate(data))  return;
				
				//appTranstype下拉框对象
				var appTranstypeSelect = document.getElementById("appTranstypeSelect");
				
				appTranstypeSelect.options.length = 0;//清空transtype下拉框
				if (data.length > 0) {
					for (var i = 0; i < data.length; i++) {
						var option = new Option();
						option.text = data[i].transName;
						option.value = data[i].transCode;
						appTranstypeSelect.add(option);
					}
					
					enabledBtn("channelTranstypeBtn");//启用按钮
				}else if(data.length == 0){
					var option = new Option();
					option.text = "无可选交易类型！";
					option.value = "";
					appTranstypeSelect.add(option);
					disabledBtn("channelTranstypeBtn");//禁用按钮
				}
			}
		});
	}
	
	//点击"删除(channelTranstype)"
	function delChannelTranstype(channelTranstype){
		var delTr = $(channelTranstype).parent('td').parent('tr')[0];
		var transtypeId = delTr.childNodes[0].innerHTML;
		var appId =  $("#appSelect_div").val();
		$.dialog({
			type: "warn",//success,error
			content: "您确定要删除该条记录吗？",
			onOk: function() {
				$.openLoading();
				//根据transtypeId删除相应的channelTranstype
				$.ajax({
					url : "<%=path%>/channelManage/delChannelTranstypeByTranstypeId?"+new Date().getTime(),
					type : 'POST',
					async : true,
					data:{
						transtypeId:transtypeId,
						channelCode:channelCode
					},
					dataType:'json',
					success : function(data) {
						$.closeLoading();
						
						if($.validate(data))  return;
						
						loadChannelTranstype(channelCode,appId);
					}
				});
			}
		});
	}
	
	//点击"新增(channelTranstype)"
	function saveChannelTranstype(){
		var transCode = $("#appTranstypeSelect").val();
		var transName = $("#appTranstypeSelect").find("option:selected").text(); 
		
		var appId = $("#appSelect_div").val();
		
		$.openLoading();
		//添加channelTranstype
		$.ajax({
			url : "<%=path%>/channelManage/saveChannelTranstype?"+new Date().getTime(),
			type : 'POST',
			async : true,
			data:{
				transCode:transCode,
				transName:transName,
				fkChannelCode:channelCode,
				appId:appId
			},
			dataType:'json',
			success : function(data) {
				$.closeLoading();
				
				if($.validate(data))  return;
				
				loadChannelTranstype(channelCode,appId);//重新加载channelTranstype表格及下拉框的内容
			}
		});
	}
	
	//禁用按钮(传入要禁用的按钮Id)
	function disabledBtn(btnId){
		var disabledBtn = $("#"+btnId+"");
		disabledBtn.attr("disabled","disabled");
		disabledBtn.css("background","gray");
	}
	//启用按钮style="background-image: url('');" 
	function enabledBtn(btnId){
		var enabledBtn = $("#"+btnId+"");
		enabledBtn.removeAttr("disabled");
		enabledBtn.css("backgroundImage","url('./images/button_1.png')");
	}
	
</script>

  </head>
  
  <body>
  
 	 <div class="current">
           <table class="current_table_1">
             <tbody>
              	<tr class="f2">
	                <td><img src="./images/current_1.jpg"></td>
	                <td class="current_table_1_td">
	                  <img src="images/current_1.png">&nbsp;您现在的位置：&nbsp;
	                  <span style=" color:#136bab; ">合作伙伴管理
	                  <span style="padding:0 5px; font-weight:bold; line-height:28px;">&gt;</span>合作伙伴配置
	                  <span style="padding:0 5px; font-weight:bold; line-height:28px;">&gt;</span>应用配置
	                  </span>
                  	</td>
                 	<td><img src="./images/current_3.jpg"></td>
              	</tr>
           	 </tbody>
           </table>
     </div>
     		<table class="table_05">
	              <tbody>
		    		<tr>
 		              <td style="line-height:23px;">
 		              		<font color="#3876bf"><b>合作伙伴代码：</b></font> ${channelCode}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#3876bf"><b>合作伙伴名称：</b></font>${channelName}
 		              </td>
		          	</tr>
	           	  </tbody>
	        </table>
			<table>
				<tr height="30px">
					<td width="100%" height="30">
						<input type="button" class="input_button_2" id="addButton" value="新增应用" onClick="addChannelAppInfo()" />&nbsp;
					</td>			
				</tr>			
			</table>		
  	 	  <!-- 已添加应用列表 -->
	      <div style="margin-top: 10px;">
	         <table class="table_02" id="channelInfoOuter" align="center" >
				<tbody>
					<tr>
						   <td colspan="6" style=" font-size:12px; font-weight:bold; text-align:left;color:#3876bf; line-height:27px;height:27px; background-image:url(images/centent_1.jpg)">&nbsp;已添加应用列表</td>
					</tr>
					<tr>
						<td>
							<table id="channelInfoTable" class="content_table_1">
								<thead>
									<tr class="f1">
		                  			   <th style="width:33%">应用接口</th>
		                               <th style="width:33%">合作方式</th>
		                               <th style="width:34%">操作</th>
					              	</tr>
								</thead>
								<tbody>
								</tbody>
							</table>
							<div id="channelInfoPageDIV"  class="page"></div>
						</td>
					</tr>
				</tbody>
			</table>
	      </div>
  	
  	<%--添加或修改ChanelInfo相关数据弹出层 --%>
  	<div id="addOrUpdateDiv" style="display: none;height: 180px;width: 680px;margin-top: -50px">
  		<div class="table_title_div">
				<table class="table_title" style='width:680px;height: 20px;'>
					<tr>
						<td width="10%"></td>
						<td width="80%" id="interfaceName"></td>
						<td width="10%" align="right"><img src="<%=path %>/images/closedown.gif" alt="关闭"  onclick="closeAddOrUpdateDiv()"/></td>
					</tr>
				</table>
		</div>
		
		<div id="channelRelatedDiv" style="height: 280px;overflow-y: scroll">
	  		<table id="channelRelatedTable" class="table_02" align="center" style="width: 600px">
	   			<tr>
	   				<td>
	   					<div id="channelDiv" style="margin:5px auto;width: 500px">
		   					<table style="border: 1px solid #bbcddb;width: 500px">
		   						<tr>
		   							<td style="border-right: 0px;text-align: right;" nowrap="nowrap">所属应用接口：</td>
		   							<td style="border-right: 0px;padding:5px 30px;text-align: left" nowrap="nowrap">
		   								<select id="appSelect_div"></select>
		   							</td>
		   						</tr>
		   						<tr>
		   							<td colspan="6" style="text-align: center;padding:8px ;border: 0px">
		   								<input type="button" class="input_button_1" id="saveChannelInfoBtn" onclick="saveOrUpdateChannelInfo()" value="新增"/>
		   							</td>
		   						</tr>
		   					</table>
		   				</div>
	   				</td>
	   			</tr>
	   			<tr>
	   				<td style="width: 600px">
						<div id="channelTranstypeDiv" style="margin:5px auto;width: 500px">
							<table id="channelTranstypeTable" class="table_02" style="width: 500px">
								<thead>
									 <tr>
						                  <th style="width: 930px">交易名称</th>
						                  <th style="width: 200px">操作</th>
						              </tr>
								</thead>
								<tfoot>
									<tr>
										<td style="padding: 10px"><select id="appTranstypeSelect"><option value="">无可选交易类型！</option></select></td>
										<td style="padding: 10px"><input class="input_button_1" type="button" id="channelTranstypeBtn" value="新增"  onClick="saveChannelTranstype()"/></td>
									</tr>
								</tfoot>
							</table>
						</div>
					</td>	
	   			</tr>
		   </table>
	   </div>
  	</div>
  </body>
</html>